﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LinqToExcel;
using System.Runtime.Serialization;
using MensUnoShop.Models;
using System.ComponentModel;
using LinqToExcelProdider;

namespace MensUnoShop.Areas.Admin.Models
{
    public class EXProductImport:MensUnoImport,IImport
    {
        public EXProductImport(HttpPostedFileBase file, ImportType it) : base(file, it) { }
        public bool IMPORT(ref string result)
        {
            bool isok = false;
            try
            {
                ExcelProvider execlFile = ExcelProvider.Create(_filepath);


                var excel = from item in execlFile.GetSheet<EXProduct>().Where(v => v.貨號 == "11004008") select item;
                _log.RecordExceptionFormat(_log.FlowLogPath, "[MKspec]", excel.FirstOrDefault().尺寸表);
                /*var mksn = excel.Select(e => e.貨號).ToList().Distinct();
                foreach (var ssn in mksn)
                {
                    string sn = ssn.Trim();
                    //看這賣場存不存在
                    Market mk = MarketFactory.GetMarket(sn);
                    if (mk == null)
                    {
                        var detail = excel.Where(d => d.貨號 == ssn).ToList();
                        var mfirst = detail.FirstOrDefault();
                        //新增賣場Start-------------
                        //確認系列標籤
                        long grouptagid = TagFactory.CheckTag(mfirst.系列標籤, Config.TagType.GROUP);
                        //確認色彩標籤
                        long colortagid = TagFactory.CheckTag(mfirst.色彩標籤, Config.TagType.COLORGOURP);
                        Guid mkguid = MarketFactory.CreateNewMarket(mfirst.貨號, mfirst.主標題, long.Parse(mfirst.次分類序號), int.Parse(mfirst.市價),
                                                                    int.Parse(mfirst.售價), DateTime.Parse(mfirst.開賣時間 ?? "2222/10/27"), DateTime.Parse(mfirst.結束時間 ?? "2222/10/27"), mfirst.洗滌注意事項,
                                                                    mfirst.購物須知,mfirst.尺寸表, mfirst.商品說明, grouptagid,
                                                                    colortagid);
                        //新增賣場End-------------
                        if (mkguid != Guid.Empty)
                        {
                            //新增商品Start-----------
                            var pd = detail.Select(p => new { p.子商品大圖, p.子商品小圖, p.顏色, p.顏色編號 }).ToList().Distinct();
                            foreach (var p in pd)
                            {
                                long tagid = TagFactory.CheckTag(p.顏色, int.Parse(p.顏色編號), Config.TagType.PRODUCT);
                                Guid pdguid = MarketFactory.CreateNewProduct(mkguid, tagid, p.子商品大圖, p.子商品小圖);
                                if (pdguid != Guid.Empty)
                                {
                                    var sp = detail.Where(d => d.貨號 == sn && d.顏色編號 == p.顏色編號).Select(s => new { s.尺寸, s.尺寸編號, s.規格數量 });
                                    foreach (var s in sp)
                                    {
                                        long tagspid = TagFactory.CheckTag(s.尺寸, int.Parse(s.尺寸編號), Config.TagType.SPEC);
                                        MarketFactory.CreateNewSpec(pdguid, tagspid, int.Parse(s.規格數量));
                                    }
                                    //新增商品End-------------
                                    result = "Success(" + sn + ")";
                                    isok = true;
                                }
                                else
                                {
                                    _log.RecordExceptionFormat(_log.FlowLogPath, "[Product Create Fail(" + sn + ")]", p.顏色編號 + p.顏色);
                                    result = "Product Create Fail(" + sn + ")";
                                    isok = false;
                                }
                            }
                                                       
                        }
                        else
                        {
                            _log.RecordExceptionFormat(_log.FlowLogPath, "[Market Create Fail(" + sn + ")]","");
                            result = "Market Create Fail(" + sn + ")";
                            isok = false;
                        }
                    }
                    else
                    { 
                        //存在賣場就更新
                        var detail = excel.Where(d => d.貨號 == ssn).ToList();
                        var mfirst = detail.FirstOrDefault();
                        //更新賣場Start-------------
                        //確認系列標籤
                        long grouptagid = TagFactory.CheckTag(mfirst.系列標籤, Config.TagType.GROUP);
                        //確認色彩標籤
                        long colortagid = TagFactory.CheckTag(mfirst.色彩標籤, Config.TagType.COLORGOURP);
                        _log.RecordExceptionFormat(_log.FlowLogPath, "[MKspec]", mfirst.尺寸表);
                        MarketFactory.UpdateMarket(mk.MKGUID, mfirst.主標題, long.Parse(mfirst.次分類序號), int.Parse(mfirst.市價),
                                                                    int.Parse(mfirst.售價), DateTime.Parse(mfirst.開賣時間 ?? "2222/10/27"), DateTime.Parse(mfirst.結束時間 ?? "2222/10/27"), mfirst.洗滌注意事項,
                                                                    mfirst.購物須知, mfirst.尺寸表, mfirst.商品說明, grouptagid,
                                                                    colortagid);
                        //更新賣場End-------------

                        var pd = detail.Select(p => new { p.子商品大圖, p.子商品小圖, p.顏色, p.顏色編號 }).ToList().Distinct();
                        foreach (var p in pd)
                        {
                            long tagid = TagFactory.CheckTag(p.顏色, int.Parse(p.顏色編號), Config.TagType.PRODUCT);
                            var mkp = mk.PRODUCTLIST.FirstOrDefault(pp => pp.TITLETAGID == int.Parse(p.顏色編號));
                            if (mkp == null)
                            {
                                //商品不存在，新增
                                Guid pdguid = MarketFactory.CreateNewProduct(mk.MKGUID, tagid, p.子商品大圖, p.子商品小圖);
                                if (pdguid != Guid.Empty)
                                {
                                    var sp = detail.Where(d => d.貨號 == sn && d.顏色編號 == p.顏色編號).Select(s => new { s.尺寸, s.尺寸編號, s.規格數量 });
                                    foreach (var s in sp)
                                    {
                                        long tagspid = TagFactory.CheckTag(s.尺寸, int.Parse(s.尺寸編號), Config.TagType.SPEC);
                                        MarketFactory.CreateNewSpec(pdguid, tagspid, int.Parse(s.規格數量));
                                    }
                                    //新增商品End-------------
                                    result = "Success(" + sn + ")";
                                    isok = true;
                                }
                                else
                                {
                                    _log.RecordExceptionFormat(_log.FlowLogPath, "[Product Create Fail(" + sn + ")]", p.顏色編號 + p.顏色);
                                    result = "Product Create Fail(" + sn + ")";
                                    isok = false;
                                }
                            }
                            else
                            { 
                                //商品存在，更新
                                mkp.PICBIG = p.子商品大圖;
                                mkp.PICSMALL = p.子商品小圖;
                                mkp.Update();
                                mkp.GetSpecification();
                                foreach (var sp in mkp.SPEC)
                                {
                                    foreach (var dp in detail)
                                    {
                                        if (int.Parse(dp.顏色編號) == mkp.TITLETAGID && int.Parse(dp.尺寸編號) == sp.TITLETAGID)
                                        {
                                            sp.UpdateStock(int.Parse(dp.規格數量));
                                        }
                                    }
                                }
                            }
                        }

                        result = "Success Update!!!";
                        isok = true;
                    }
                }*/
                
            }
            catch (Exception ex)
            {
                result = ex.ToString();
                isok = false;
            }
            return isok;
        }
    }



[ExcelSheet(Name="Product")]
public class EXProduct: INotifyPropertyChanged
{

	private string _貨號;
	private string _主標題;
	private string _次分類序號;
	private string _市價;
	private string _售價;
	private string _開賣時間;
    private string _結束時間;
	private string _尺寸表;
	private string _洗滌注意事項;
	private string _購物須知;
	private string _商品說明;
	private string _系列標籤;
	private string _色彩標籤;
	private string _子商品大圖;
	private string _子商品小圖;
	private string _顏色編號;
	private string _顏色;
	private string _尺寸編號;
	private string _尺寸;
	private string _規格數量;

	public event PropertyChangedEventHandler PropertyChanged;

	protected virtual void SendPropertyChanged(string propertyName)
	{
		PropertyChangedEventHandler handler = PropertyChanged;
		if (handler != null) {
			handler(this, new PropertyChangedEventArgs(propertyName));
		}
	}


	[ExcelColumn(Name="貨號", Storage="_貨號")]
	public string 貨號
	{
		get { return _貨號;}
		set {
			_貨號 = value; 
			SendPropertyChanged("貨號");
		}
	}

	[ExcelColumn(Name="主標題", Storage="_主標題")]
	public string 主標題
	{
		get { return _主標題;}
		set {
			_主標題 = value; 
			SendPropertyChanged("主標題");
		}
	}

	[ExcelColumn(Name="次分類序號", Storage="_次分類序號")]
	public string 次分類序號
	{
		get { return _次分類序號;}
		set {
			_次分類序號 = value; 
			SendPropertyChanged("次分類序號");
		}
	}

	[ExcelColumn(Name="市價", Storage="_市價")]
	public string 市價
	{
		get { return _市價;}
		set {
			_市價 = value; 
			SendPropertyChanged("市價");
		}
	}

	[ExcelColumn(Name="售價", Storage="_售價")]
	public string 售價
	{
		get { return _售價;}
		set {
			_售價 = value; 
			SendPropertyChanged("售價");
		}
	}

	[ExcelColumn(Name="開賣時間", Storage="_開賣時間")]
    public string 開賣時間
	{
		get { return _開賣時間;}
		set {
			_開賣時間 = value; 
			SendPropertyChanged("開賣時間");
		}
	}

	[ExcelColumn(Name="結束時間", Storage="_結束時間")]
    public string 結束時間
	{
		get { return _結束時間;}
		set {
			_結束時間 = value; 
			SendPropertyChanged("結束時間");
		}
	}

	[ExcelColumn(Name="尺寸表", Storage="_尺寸表")]
	public string 尺寸表
	{
		get { return _尺寸表;}
		set {
			_尺寸表 = value; 
			SendPropertyChanged("尺寸表");
		}
	}

	[ExcelColumn(Name="洗滌注意事項", Storage="_洗滌注意事項")]
	public string 洗滌注意事項
	{
		get { return _洗滌注意事項;}
		set {
			_洗滌注意事項 = value; 
			SendPropertyChanged("洗滌注意事項");
		}
	}

	[ExcelColumn(Name="購物須知", Storage="_購物須知")]
	public string 購物須知
	{
		get { return _購物須知;}
		set {
			_購物須知 = value; 
			SendPropertyChanged("購物須知");
		}
	}

	[ExcelColumn(Name="商品說明", Storage="_商品說明")]
	public string 商品說明
	{
		get { return _商品說明;}
		set {
			_商品說明 = value; 
			SendPropertyChanged("商品說明");
		}
	}

	[ExcelColumn(Name="系列標籤", Storage="_系列標籤")]
	public string 系列標籤
	{
		get { return _系列標籤;}
		set {
			_系列標籤 = value; 
			SendPropertyChanged("系列標籤");
		}
	}

	[ExcelColumn(Name="色彩標籤", Storage="_色彩標籤")]
	public string 色彩標籤
	{
		get { return _色彩標籤;}
		set {
			_色彩標籤 = value; 
			SendPropertyChanged("色彩標籤");
		}
	}

	[ExcelColumn(Name="子商品大圖", Storage="_子商品大圖")]
	public string 子商品大圖
	{
		get { return _子商品大圖;}
		set {
			_子商品大圖 = value; 
			SendPropertyChanged("子商品大圖");
		}
	}

	[ExcelColumn(Name="子商品小圖", Storage="_子商品小圖")]
	public string 子商品小圖
	{
		get { return _子商品小圖;}
		set {
			_子商品小圖 = value; 
			SendPropertyChanged("子商品小圖");
		}
	}

	[ExcelColumn(Name="顏色編號", Storage="_顏色編號")]
	public string 顏色編號
	{
		get { return _顏色編號;}
		set {
			_顏色編號 = value; 
			SendPropertyChanged("顏色編號");
		}
	}

	[ExcelColumn(Name="顏色", Storage="_顏色")]
	public string 顏色
	{
		get { return _顏色;}
		set {
			_顏色 = value; 
			SendPropertyChanged("顏色");
		}
	}

	[ExcelColumn(Name="尺寸編號", Storage="_尺寸編號")]
	public string 尺寸編號
	{
		get { return _尺寸編號;}
		set {
			_尺寸編號 = value; 
			SendPropertyChanged("尺寸編號");
		}
	}

	[ExcelColumn(Name="尺寸", Storage="_尺寸")]
	public string 尺寸
	{
		get { return _尺寸;}
		set {
			_尺寸 = value; 
			SendPropertyChanged("尺寸");
		}
	}

	[ExcelColumn(Name="規格數量", Storage="_規格數量")]
	public string 規格數量
	{
		get { return _規格數量;}
		set {
			_規格數量 = value; 
			SendPropertyChanged("規格數量");
		}
	}
}




   
}